package jourre;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import entity.Pubmed;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.PubmedUtils2;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-3-30 15:46
 */
public class adddata {

    public static String localPm = "pubmed_vip_extend_0530";
    //    static String dbName2 = "xml_content_3qw";
    static String dbName2 = "xml_content_1_3qw";
    // 全部插入不判断 如果是true 则进行全部插入
    static boolean insert = true;
    //是不是要更新
    static boolean isupdate = false;
    //通过id进行开始


    @SneakyThrows
    public static void main(String[] args) {
        Db use = Db.use(DSFactory.get("server_local"));
        File file = new File("");
        String PATH = file.getCanonicalPath() + "\\src\\main\\java\\jourre\\" + "pmid";
        List<String> strings = FileUtil.readLines(PATH, "UTF-8");
        String join = String.join(",", strings);
        String paramSql = "select * from " + dbName2 + " where  pmid in (  " + join + " ) ";
        List<Entity> querys = use.query(paramSql);
        querys.parallelStream().forEach(entity -> {
            String content = ZipUtil.unGzip(entity.getBytes("content"), "utf-8");
            //解析html
            Document doc = Jsoup.parse(content);
            try {
                // 主要是用于进行过滤掉参文房子参文doi混入文章doi
                doc.select("ReferenceList").remove();
            } catch (Exception e) {

            }
            List<Element> divList = new ArrayList<>();
            Elements pubmedArticles = doc.select("PubmedArticle");
            Elements pubmedBookArticles = doc.select("PubmedBookArticle");
            for (Element e : pubmedArticles) {
                divList.add(e);
            }
            for (Element e : pubmedBookArticles) {
                divList.add(e);
            }
            List<Pubmed> pubmeds = PubmedUtils2.parseHtmlAddAuAdd(divList);
            //直接入库
            for (Pubmed pubmed : pubmeds) {
                try {
                    insertData(pubmed, use);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }

    @SneakyThrows
    public static void insertData(Pubmed pubmed, Db use) {
        if (insert) {
            use.insert(new Entity().setTableName(localPm)
                    .set("pmid", pubmed.getPmid())
                    .set("type", pubmed.getType())
                    .set("article_title", pubmed.getArticleTitle())
                    .set("source", pubmed.getSource())
                    .set("intro", pubmed.getIntro())
                    .set("jour", pubmed.getJour())
                    .set("volume", pubmed.getVolume())
                    .set("issue", pubmed.getIssue())
                    .set("pages", pubmed.getPages())
                    .set("vip", pubmed.getVip())
                    .set("pub_date", pubmed.getPubDate())
                    .set("medline_date", pubmed.getMedlineDate())
                    .set("pub_year", pubmed.getPubYear())
                    .set("med_nlm_id", pubmed.getMedNlmId())
                    .set("author", pubmed.getAuthor())
                    .set("author_extend", pubmed.getAuthorExtend())
                    .set("author_add", pubmed.getAuthorAdd())
                    .set("ischina", pubmed.getIschina())
                    .set("affs", pubmed.getAffs())
                    .set("pmcid", pubmed.getPmcid())
                    .set("doi", pubmed.getDoi())
                    .set("lan", pubmed.getLan())
                    .set("ab_text", pubmed.getAbText())
                    .set("copyright", pubmed.getCopyright())
                    .set("keyword", pubmed.getKeyword())
                    .set("pts", pubmed.getPts())
                    .set("meshs", pubmed.getMeshs())
                    .set("mesh_heading", pubmed.getMeshHeading())
                    .set("mesh_headingy", pubmed.getMeshHeadingy())
                    .set("subs", pubmed.getSubs())
                    .set("grants", pubmed.getGrants())
                    .set("ass_date", pubmed.getAssDate())
                    .set("full_text", pubmed.getFullText())
                    .set("art_type", pubmed.getArtType())
            );
        } else {
            String sqlParam = "select pmid from " + localPm + " where pmid = ?";
            Entity entity = use.queryOne(sqlParam, pubmed.getPmid());
            if (entity == null) {
                try {
                    use.insert(new Entity().setTableName(localPm)
                            .set("pmid", pubmed.getPmid())
                            .set("type", pubmed.getType())
                            .set("article_title", pubmed.getArticleTitle())
                            .set("source", pubmed.getSource())
                            .set("intro", pubmed.getIntro())
                            .set("jour", pubmed.getJour())
                            .set("volume", pubmed.getVolume())
                            .set("issue", pubmed.getIssue())
                            .set("pages", pubmed.getPages())
                            .set("vip", pubmed.getVip())
                            .set("pub_date", pubmed.getPubDate())
                            .set("medline_date", pubmed.getMedlineDate())
                            .set("pub_year", pubmed.getPubYear())
                            .set("med_nlm_id", pubmed.getMedNlmId())
                            .set("author", pubmed.getAuthor())
                            .set("author_extend", pubmed.getAuthorExtend())
                            .set("author_add", pubmed.getAuthorAdd())
                            .set("ischina", pubmed.getIschina())
                            .set("affs", pubmed.getAffs())
                            .set("pmcid", pubmed.getPmcid())
                            .set("doi", pubmed.getDoi())
                            .set("lan", pubmed.getLan())
                            .set("ab_text", pubmed.getAbText())
                            .set("copyright", pubmed.getCopyright())
                            .set("keyword", pubmed.getKeyword())
                            .set("pts", pubmed.getPts())
                            .set("meshs", pubmed.getMeshs())
                            .set("mesh_heading", pubmed.getMeshHeading())
                            .set("mesh_headingy", pubmed.getMeshHeadingy())
                            .set("subs", pubmed.getSubs())
                            .set("grants", pubmed.getGrants())
                            .set("ass_date", pubmed.getAssDate())
                            .set("full_text", pubmed.getFullText())
                            .set("art_type", pubmed.getArtType())
                    );
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {

                if (isupdate) {
                    Entity delE = new Entity();
                    delE.setTableName(localPm);
                    delE.set("pmid", pubmed.getPmid());
                    Entity entity1 = new Entity();
                    entity1.set("pmid", pubmed.getPmid())
                            .set("type", pubmed.getType())
                            .set("article_title", pubmed.getArticleTitle())
                            .set("source", pubmed.getSource())
                            .set("intro", pubmed.getIntro())
                            .set("jour", pubmed.getJour())
                            .set("volume", pubmed.getVolume())
                            .set("issue", pubmed.getIssue())
                            .set("pages", pubmed.getPages())
                            .set("vip", pubmed.getVip())
                            .set("pub_date", pubmed.getPubDate())
                            .set("medline_date", pubmed.getMedlineDate())
                            .set("pub_year", pubmed.getPubYear())
                            .set("med_nlm_id", pubmed.getMedNlmId())
                            .set("author", pubmed.getAuthor())
                            .set("author_extend", pubmed.getAuthorExtend())
                            .set("author_add", pubmed.getAuthorAdd())
                            .set("ischina", pubmed.getIschina())
                            .set("affs", pubmed.getAffs())
                            .set("pmcid", pubmed.getPmcid())
                            .set("doi", pubmed.getDoi())
                            .set("lan", pubmed.getLan())
                            .set("ab_text", pubmed.getAbText())
                            .set("copyright", pubmed.getCopyright())
                            .set("keyword", pubmed.getKeyword())
                            .set("pts", pubmed.getPts())
                            .set("meshs", pubmed.getMeshs())
                            .set("mesh_heading", pubmed.getMeshHeading())
                            .set("mesh_headingy", pubmed.getMeshHeadingy())
                            .set("subs", pubmed.getSubs())
                            .set("grants", pubmed.getGrants())
                            .set("ass_date", pubmed.getAssDate())
                            .set("full_text", pubmed.getFullText())
                            .set("art_type", pubmed.getArtType());
                    use.update(entity1, delE);
                }

            }
        }
    }


}
